#include "histogram.h"
#include "format_converter.h"


histogram::histogram(QWidget *parent, PBO* pbo) : QWidget(parent)
{
	this->pbo = pbo;

	ui.setupUi(this);
	imageWindow = new QLabel(this);
	imageWindow->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

	this->setFixedSize(256, 150);

	QPushButton* redButton = new QPushButton("R", this);
	redButton->move(0,120);
	redButton->resize(20,20);
	QPushButton* greenButton = new QPushButton("G", this);
	greenButton->move(25,120);
	greenButton->resize(20,20);
	QPushButton* blueButton = new QPushButton("B", this);
	blueButton->move(50,120);
	blueButton->resize(20,20);
	QPushButton* grayButton = new QPushButton("Gray", this);
	grayButton->move(75,120);
	grayButton->resize(50,20);

	QObject::connect(redButton,SIGNAL(clicked()), this,SLOT(countRed()));
	QObject::connect(greenButton,SIGNAL(clicked()), this,SLOT(countGreen()));
	QObject::connect(blueButton,SIGNAL(clicked()), this,SLOT(countBlue()));
	QObject::connect(grayButton,SIGNAL(clicked()), this,SLOT(countGray()));

	QPushButton* flattenButton = new QPushButton("Flatten", this);
	flattenButton->move(130,120);
	flattenButton->resize(50,20);
	QPushButton* balancingButton = new QPushButton("Balancing", this);
	balancingButton->move(185,120);
	balancingButton->resize(70,20);
	
	QObject::connect(flattenButton,SIGNAL(clicked()), this,SLOT(flatten()));
	QObject::connect(balancingButton,SIGNAL(clicked()), this,SLOT(balancing()));


	QObject::connect(pbo, SIGNAL(bufferChanged()), this, SLOT(count()));

	type = HISTOGRAM_GRAY;
	count();
}

void histogram::countRed()
{
	type = HISTOGRAM_RED;
	count();
}

void histogram::countGreen()
{
	type = HISTOGRAM_GREEN;
	count();
}

void histogram::countBlue()
{
	type = HISTOGRAM_BLUE;
	count();
}

void histogram::countGray()
{
	type = HISTOGRAM_GRAY;
	count();
}

void histogram::count()
{
	int hist_size = 256;
	unsigned int * test = pbo->histogram(type);
	Image* hist = new Image();
	hist->setVersion("P4");
	hist->colorRange = MONO;
	hist->max_value = 1;
	hist->width = hist_size;
	hist->height = 100;

	hist->data = new unsigned char[hist->width * hist->height];	
	for(int i=0;i<hist->width * hist->height;i++) hist->data[i] = 1;

	int max = 0;
	for(int i=0;i<hist_size;i++)
	{
		if(test[i]>max) max = test[i];
	}

	for(int x=0;x<hist_size;x++)
	{
		int wys = (hist->height - (((double)test[x]/(double)max) * hist->height));
		for(int y = hist->height-1; y > wys; y--)
		{
			if(y<=0)
			{
				qDebug("Histogram out of range!!");
				continue;
			}

			int offset = y * hist->width + x;
			hist->data[offset] = 0;
		}
	}
	QImage histo = FormatConverter::toQImage(hist);
	const QPixmap pixmap = QPixmap::fromImage(histo);
	imageWindow->setPixmap(pixmap);
	imageWindow->setFixedSize(hist->width, hist->height);
	imageWindow->show();
}

void histogram::flatten()
{
	pbo->flatten();
}

void histogram::balancing()
{
	pbo->balancing();
}

histogram::~histogram()
{

}
